/*
 * Copyright 2018-present KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nsgf.library.ui.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.NonNull;

import com.nsgf.library.R;
import com.nsgf.library.data.bean.WorkOrderDetailBean;
import com.nsgf.library.databinding.ItemManagerWorkOrderItemBinding;
import com.nsgf.library.matisse.MimeType;
import com.nsgf.library.photopicker.widget.MultiPickResultView;
import com.nsgf.library.photopicker.widget.PhotoAdapter;
import com.nsgf.library.ui.page.manager.ManagerWorkOrderFromSubmitActivity;
import com.nsgf.library.widget.CustomSwitch;
import com.nsgf.library.widget.prckerview.OptionsPickerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by GC at 24/10/19
 */
public class ManagerDamTaskListAdapter extends SimpleDataBindingPositionAdapter<WorkOrderDetailBean.DataBean.PositionVOSBean.ItemsBean,
        ItemManagerWorkOrderItemBinding> {
    ManagerWorkOrderFromSubmitActivity activity;

    public ManagerDamTaskListAdapter(ManagerWorkOrderFromSubmitActivity context) {
        super(context, R.layout.item_manager_work_order_item, DiffUtils.getInstance().getmWorkOrderDetailTaskItemCallback());
        this.index = 0;
        this.activity = context;
    }

    public List<MultiPickResultView> multiPickResultViewList = new ArrayList<>();

    private int index;

    public List<MultiPickResultView> getMultiPickResultViewList() {
        return multiPickResultViewList;
    }

    public List<ViewHolder> itemBindings = new ArrayList<>();

    @Override
    protected void onBindItem(ItemManagerWorkOrderItemBinding binding,
                              WorkOrderDetailBean.DataBean.PositionVOSBean.ItemsBean item,
                              ViewHolder holder, int position) {
        binding.setAlbum(item);
        itemBindings.add(holder);


        if ("txt".equals(item.getResultType())) {
            binding.itemNsgfSwitch.setVisibility(View.GONE);
            item.setViewGone(View.VISIBLE);
            binding.multiPickResultView.setVisibility(View.GONE);
        } else {
            if (item.getOptions().size() > 2) {
                binding.tvItemWorkOrderCheckbox.setVisibility(View.VISIBLE);
                binding.itemNsgfSwitch.setVisibility(View.GONE);
                binding.tvItemWorkOrderCheckbox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        OptionsPickerView<String> mOptionsPickerView = new OptionsPickerView.Builder(mContext, new OptionsPickerView.OnOptionsSelectListener() {
                            @Override
                            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                                binding.tvItemWorkOrderCheckbox.setText(item.getOptions().get(options1));
                                item.setResultValue(item.getOptions().get(options1));
                                if (item.getOptions().get(options1).equals(item.getValueSrc())) {
                                    item.setViewGone(View.GONE);
                                } else
                                    item.setViewGone(View.VISIBLE);
                            }
                        }).build();
                        mOptionsPickerView.setPicker(item.getOptions());
                        mOptionsPickerView.show();
                    }
                });
            } else {
                binding.itemNsgfSwitch.setVisibility(View.VISIBLE);
                binding.tvItemWorkOrderCheckbox.setVisibility(View.GONE);

                if (item.getOptions().size() > 1) {
                    binding.itemNsgfSwitch.setCloseText(item.getOptions().get(0));
                    binding.itemNsgfSwitch.setOpenText(item.getOptions().get(1));
                } else {
                    binding.itemNsgfSwitch.setCloseText("否");
                    binding.itemNsgfSwitch.setOpenText("是");
                }
                binding.itemNsgfSwitch.setOnCheckedChangeListener(new CustomSwitch.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(boolean isChecked) {

                        if (isChecked) {
                            item.setResultValue(item.getOptions().get(1));
                        } else {
                            item.setResultValue(item.getOptions().get(0));
                        }
                        item.setCheck(isChecked);
                        item.setViewGone(isChecked ? View.VISIBLE : View.GONE);
                    }
                });
            }
        }

        binding.multiPickResultView.init(activity,
                MultiPickResultView.ACTION_SELECT,
                (ArrayList<WorkOrderDetailBean.FileDto>) item.getLiveImageList(),
                5,
                position,
                MimeType.ofAll(), position1 -> {
                    if (null != item.getLiveImageList())
                        if (item.getLiveImageList().size() >= position1)
                            item.getLiveImageList().remove(position1);
                });
        multiPickResultViewList.add(binding.multiPickResultView);
        photoAdapters.add(binding.multiPickResultView.photoAdapter);


        binding.etNsgfManagerAbnormalDescribe.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                // 当文本改变时更新长度显示
                int length = charSequence.length();
                binding.textView.setText(length + "/500");
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    public List<PhotoAdapter> photoAdapters = new ArrayList<>();

    public List<PhotoAdapter> getPhotoAdapters() {
        return photoAdapters;
    }

    @NonNull
    @Override
    public List<WorkOrderDetailBean.DataBean.PositionVOSBean.ItemsBean> getCurrentList() {
        return super.getCurrentList();
    }

    public int getIndex() {
        return index;
    }

}
